<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Upload file for uploading files into the theme file repository
 *
 * This file is necessary because there is currently no other moodle way
 * to get files into a file system repository and we want to maintain
 * the easy of use of FTPing custom images to the server.
 *
 * @package   theme-rlmaster
 * @copyright 2011 Remote Learner  http://www.remote-learner.net/
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require_once('../../config.php');
require_once($CFG->libdir .'/adminlib.php');
require_once($CFG->libdir .'/uploadlib.php');

$name      = basename(__DIR__);
$themename = 'theme_'. $name;

// $PAGE must be set up before admin_get_root()
$PAGE->set_context(null);
$PAGE->set_url('/theme/'. $name .'/upload.php');
$PAGE->set_pagelayout('popup');

// Check permissions
require_login(SITEID, false);
$adminroot = admin_get_root(); // need all settings
$settingspage = $adminroot->locate('themesetting'. $name, true);

if (empty($settingspage) or !($settingspage instanceof admin_settingpage)) {
    print_error('sectionerror', 'admin', "$CFG->wwwroot/$CFG->admin/");
    die;
}

if (!($settingspage->check_access())) {
    print_error('accessdenied', 'admin');
    die;
}

// Check file and move to permanent home.
$errors = '<p class="centered">'. get_string('uploadnone', $themename) .'</p>';

if (array_key_exists('theme_file_upload', $_FILES)) {
    $manager = new upload_manager('theme_file_upload');
    $manager->save_files($CFG->dataroot .'/repository/'. $themename);
    $errors = $manager->get_errors();

    if ($errors === null) {
        $errors = '<p class="notifysuccess">'. get_string('uploadnoerrors', $themename) .'</p>';
    }
}

echo $OUTPUT->header();
echo $OUTPUT->box($errors, 'generalbox '. $name .'_theme_file_upload_message');
?>
<div class="centered">
<form id="adminsettings" action="upload.php" enctype="multipart/form-data" method="post">
<fieldset class="rlmaster_theme_file_upload_form">
<input type="file" name="theme_file_upload" />
<input type="submit" value="<?php echo get_string('upload', $themename); ?>" />
</fieldset>
</form>
</div>
<?php
echo $OUTPUT->footer();